package com.snail.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.Client;
import com.aliyun.green20220302.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Monica
 * @description
 * @date 2025/8/20 14:11
 */
@Component
public class AliYunGreenUtils {
    @Autowired
    private Client aliyunGreenClient;

    public GreenResponse reviewVideo(String url) {
        JSONObject serviceParameters = new JSONObject();
        serviceParameters.put("url", url);

        VideoModerationRequest videoModerationRequest = new VideoModerationRequest();
        // 检测类型：videoDetection
        videoModerationRequest.setService("videoDetection");
        videoModerationRequest.setServiceParameters(serviceParameters.toJSONString());
        GreenResponse greenResponse = new GreenResponse();
        try {
            VideoModerationResponse response = aliyunGreenClient.videoModeration(videoModerationRequest);

            if (response.getStatusCode() == 200) {
                VideoModerationResponseBody result = response.getBody();
                Integer code = result.getCode();
                if (200 == code) {
                    VideoModerationResponseBody.VideoModerationResponseBodyData data = result.getData();
                    greenResponse.setCode(code);
                    greenResponse.setTaskId(data.getTaskId());
                    greenResponse.setMessage(result.getMessage());
                    return greenResponse;
                } else {
                    greenResponse.setCode(code);
                    greenResponse.setMessage(result.getMessage());
                    return greenResponse;
                }
            } else {
                greenResponse.setCode(response.getStatusCode());
                return greenResponse;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return greenResponse;
    }
    // 图片不在公网，不能校验
//    public GreenResponse reviewPic(String url) {
//        RuntimeOptions runtime = new RuntimeOptions();
//        Map<String, String> serviceParameters = new HashMap<>();
//        serviceParameters.put("imageUrl", "http://172.172.2.99:9000/pic/2025/08/16/a44cec27c4ffe7e8558e5190863c8b54.jpg");
//        serviceParameters.put("dataId", UUID.randomUUID().toString());
//        ImageAsyncModerationRequest imageAsyncModerationRequest = new ImageAsyncModerationRequest();
//        imageAsyncModerationRequest.setService("baselineCheck");
//        imageAsyncModerationRequest.setServiceParameters(JSON.toJSONString(serviceParameters));
//        ImageAsyncModerationResponse response = null;
//        GreenResponse greenResponse = new GreenResponse();
//        try {
//            response = aliyunGreenClient.imageAsyncModerationWithOptions(imageAsyncModerationRequest, runtime);
//            if (response != null) {
//                if (response.getStatusCode() == 200) {
//                    ImageAsyncModerationResponseBody body = response.getBody();
//                    if (body.getCode() == 200) {
//                        ImageAsyncModerationResponseBody.ImageAsyncModerationResponseBodyData data = body.getData();
//                        greenResponse.setCode(response.getStatusCode());
//                        greenResponse.setMessage(body.getMsg());
//                        return greenResponse;
//                    } else {
//                        greenResponse.setCode(body.getCode());
//                        greenResponse.setMessage(body.getMsg());
//                        return greenResponse;
//                    }
//                } else {
//                    greenResponse.setCode(response.getStatusCode());
//                    return greenResponse;
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return greenResponse;
//    }

}
